package com.jzo2o.customer.controller.consumer;

import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Author: wen
 * @Time: 2024-04-24 22:35
 * @Version: 1.0
 */

@RestController("consumerAddressBookController")
@RequestMapping("/consumer/address-book")
@Api(tags = "用户端 - 地址薄相关接口")
public class AddressBookController {

    @Resource
    private IAddressBookService addressBookService;

    /**
     * 地址薄分页查询
     *
     * @param dto
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("地址薄分页查询")
    public PageResult<AddressBookResDTO> pageQuery(AddressBookPageQueryReqDTO dto) {
        return addressBookService.pageQuery(dto);
    }

    /**
     * 新增地址薄
     *
     * @param dto
     */
    @PostMapping
    @ApiOperation("新增地址薄")
    public void addAddressBook(@RequestBody AddressBookUpsertReqDTO dto) {
        addressBookService.addAddressBook(dto);
    }

    /**
     * 根据id查询地址薄
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址薄")
    @ApiImplicitParam(name = "id", value = "地址薄id", required = true, dataTypeClass = Long.class)
    public AddressBookResDTO getAddressById(@NotNull @PathVariable Long id) {
        return addressBookService.getAddressById(id);
    }


    /**
     * 地址薄修改
     *
     * @param id  前端传来的ID
     * @param dto 修改后的数据封装为Dto
     */
    @PutMapping("/{id}")
    @ApiOperation("地址薄修改")
    @ApiImplicitParam(name = "id", value = "地址薄id", required = true, dataTypeClass = Long.class)
    public void editAddressBook(@NotNull(message = "id不能为空") @PathVariable("id") Long id,
                                @RequestBody AddressBookUpsertReqDTO dto) {
        addressBookService.editAddressBook(id, dto);
    }


    /**
     * 批量删除地址薄。
     *
     * @param ids 地址薄ID列表，是一个不可为空的列表，每个ID代表一个要删除的地址薄记录。
     * @apiNote 此操作是不可逆的，一旦删除，对应的地址薄信息将无法恢复。
     */
    @DeleteMapping("/batch")
    @ApiOperation("批量删除地址薄")
    @ApiImplicitParam(name = "id", value = "地址薄id列表", required = true, dataTypeClass = List.class)
    public void deleteAddressBook(@NotNull(message = "id列表不能为空")
                                  @RequestBody List<Long> ids) {
        // 调用服务层方法，执行批量删除操作
        addressBookService.deleteAddressBook(ids);
    }

    /**
     * 设置地址薄为默认地址
     *
     * @param id   地址薄id
     * @param flag 是否为默认地址，0：否，1：是
     */
    @PutMapping("/default")
    @ApiOperation("地址薄设为默认/取消默认")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "地址薄id", required = true, dataTypeClass = Long.class),
            @ApiImplicitParam(name = "flag", value = "是否为默认地址，0：否，1：是", required = true, dataTypeClass = Integer.class)
    })
    public void isDefault(@NotNull(message = "id不能为空") @RequestParam Long id,
                          @NotNull(message = "flag不能为空") @RequestParam Short flag) {
        addressBookService.isDefault(id, flag);
    }

    /**
     * 获取默认地址
     *
     * @return all
     */
    @GetMapping("/defaultAddress")
    @ApiOperation("获取默认地址")
    public AddressBookResDTO defaultAddress() {
        return addressBookService.defaultAddress();
    }

}
